//订单列表
var app = getApp(); //全局APP

Page({
	//页面初始数据
	data: {
		apply: true,
		my: false,
		listData: [],
		listLog: [],
		selectNum: 0,
		checkboxAll: false,
		buttonDisabled: false,
		currentTab: 'apply',
		nodata: false,
		btnExportDisabled: false // 按钮是否可用
	},

	//加载执行
	onLoad: function(e) {
		this.setData({
			btnExportDisabled: false,
			currentTab: 'apply'
		});
		
		this.apply();
	},

	//刷新页面
	onShow: function() {
		if(this.data.currentTab == 'apply') {
			this.getApplyClassList({});
		}
	},
	
	// 我的方案
	apply: function(e) {
		this.setData({
			apply: true,
			my: false,
			nodata: false,
			listData: [],
			currentTab: 'apply'
		});
		
		this.getApplyClassList({});
	},
	
	// 我的方案
	log: function(e) {
		this.setData({
			apply: false,
			my: true,
			nodata: false,
			listLog: [], // 我的方案列表,
			currentTab: 'log'
		});
	 
		this.getApplyLogList({});
	},
	
	getApplyClassList: function(data) {
		var page = this;
		
		// 获取待提交班级列表
		app.api.getApplyClassList(data, function(res) {
			if (res.status) {
				page.setData({
					listData: res.data
				});
				
				if (res.data.length < 1) {
					nodata = true;
				}
				
				page.isAllCheckbox();
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					listData: [],
					checkboxAll: false,
					selectNum: 0,
					nodata: true
				});
			}
		});
	},

	// 设计方案
	getApplyLogList: function(data) {
		var page = this; 
		
		app.api.getApplyLogList(data, function(res) {
			if (res.status) {
				page.setData({
					listLog: res.data
				});
				 
				page.isAllCheckbox();
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					listLog: [],
					checkboxAll: false,
					selectNum: 0,
					nodata: true
				});
			}
		});
	},
	 
	//转发方案
	share: function(e) {
		var id = e.currentTarget.dataset.id;
		var dir = e.currentTarget.dataset.dir;
		var className = e.currentTarget.dataset.classname;
		var year = e.currentTarget.dataset.year;
		var season = e.currentTarget.dataset.season;
		var page = this;
		
		if(page.data.btnExportDisabled) {
			return;
		}
		
		page.setData({
			btnExportDisabled: true
		});
		
		var data = {
			id: id
		};
		
		// 提交请求
		app.api.exportExcel(data, function(res) {
			if (res.status) {
				var strFileName = year + season + '_' + dir + '_' + className + '.xls'; //文件名带后缀
				const newPath = `${wx.env.USER_DATA_PATH}/${strFileName}`;
				page.deletContract();
				wx.downloadFile({
					url: res.url,
					filePath: newPath,
					success: function(res) {
						const filePath = res.tempFilePath;
						wx.openDocument({
							filePath: newPath,
							showMenu: true,
							fileType: 'xls',
							success: function(res) {}
						})
					},
					fail: function(res) {
						wx.hideLoading();
					}
				})
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
			}
			
			page.setData({
				btnExportDisabled: false
			});
		});
		
		return false;
	},

	// 删除本地文件
	deletContract() {
		try {
			let file = wx.getFileSystemManager();
			file.readdir({
				dirPath: `${wx.env.USER_DATA_PATH}`,
				success: res => {
					if (res.files.length > 2) {
						file.unlink({
							filePath: `${wx.env.USER_DATA_PATH}/${res.files[0]}`,
							complete: res => {

							}
						})
					}
				}
			})
		} catch (error) {

		}
	},
	
	// 选择事件处理
	itemSelected: function(e) {
		var page = this;
		var plan_class_id = e.currentTarget.dataset.plan_class_id;
	
		var listData = page.data.listData;
		for (var i = 0; i < listData.length; i++) {
			if (listData[i].plan_class_id == plan_class_id) {
				if (listData[i].status == true) {
					listData[i].status = false;
				} else {
					listData[i].status = true;
				}
			}
		}
	
		page.setData({
			listData: listData
		});
	
		page.isAllCheckbox();
	},
	
	//是否全选
	isAllCheckbox: function() {
		var page = this;
		var listData = page.data.listData;
		var flag = true;
		var num = 0;
	
		if (!listData && typeof(listData) != "undefined") {
			flag = false;
			num = 0;
		} else {
			for (var i = 0; i < listData.length; i++) {
				if (listData[i].status == false) {
					flag = false;
				} else {
					num += parseInt(listData[i].num);
				}
			}
	
			if (listData.length <= 0) {
				flag = false;
				num = 0;
			}
		}
	
		this.setData({
			checkboxAll: flag,
			selectNum: num
		});
	},
	
	//全选操作
	checkboxAll: function(e) {
		if (this.data.checkboxAll == true) {
			this.setData({
				checkboxAll: false
			});
			this.setAllCheckbox(false);
		} else {
			this.setData({
				checkboxAll: true
			});
			this.setAllCheckbox(true);
		}
	},
	
	//全选设置
	setAllCheckbox: function(e) {
		var page = this;
		var listData = page.data.listData;
		if (e) {
			//全选
			for (var i = 0; i < listData.length; i++) {
				listData[i].status = true;
			}
		} else {
			//全不选
			for (var i = 0; i < listData.length; i++) {
				listData[i].status = false;
			}
		}
		page.setData({
			listData: listData
		});
	
		page.isAllCheckbox();
	},
	
	showDetail: function(e) {
		var page = this;
		var plan_class_id = e.currentTarget.dataset.plan_class_id;
	
		wx.navigateTo({
			url: '../BookAplyDetailEx/BookAplyDetailEx?plan_class_id=' + plan_class_id
		});
	},
	
	showLogDetail: function(e) {
		var page = this;
		var id = e.currentTarget.dataset.id;
		var plan_class_id = e.currentTarget.dataset.plan_class_id;
	
		wx.navigateTo({
			url: '../BookAplyLogDetailEx/BookAplyLogDetailEx?id=' + id + '&plan_class_id=' + plan_class_id
		});
	},
	
	submit: function(e) {
		var page = this; 
		
		if(page.data.selectNum == 0) {
			app.common.errorToBack("没有需要提交的学生！", 0);
			return;
		}
		
		var listData = page.data.listData;
		
		var newData = '';
		for (var i = 0; i < listData.length; i++) {
			if (listData[i].status == true) {
				// 判断预发放剩余套数，如果大于0则不能提交
				if(listData[i].yffsy > 0) {
					app.common.errorToBack(listData[i].plan_class_name + "预发放教材尚未发放完毕！", 0);
					return;
				}
				
				newData += ',' + listData[i].plan_class_id;
			} 
		}
		
		app.common.buttonDisabled(this);
		
		if (newData.substr(0, 1) == ',') {
			newData = newData.substr(1);
		}
		
		var data = {
			plan_class_ids: newData
		};
		
		// 提交申请
		app.api.bookApply(data, function(res) {
			app.common.successToShow(res.msg, function() {
				page.getApplyClassList({});
			});
		});
	},
});
